package org.example.dic.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.dic.core.entity.DicInfo;
import org.example.dic.core.entity.DicType;
import org.example.dic.core.form.QueryDicTypeListForm;
import org.example.dic.core.form.UpdateDicTypeForm;
import org.example.dic.core.mapper.DicInfoMapper;
import org.example.dic.core.mapper.DicTypeMapper;
import org.example.dic.core.service.IDicTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.system.common.constant.BaseConstant;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 数据字典类型表  服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-05-13
 */
@Service
public class DicTypeServiceImpl extends ServiceImpl<DicTypeMapper, DicType> implements IDicTypeService {
    @Resource
    DicTypeMapper dicTypeMapper;
    @Resource
    DicInfoMapper dicInfoMapper;
    @Resource
    DicCacheService dicCacheService;

    @Cacheable(value = "cache1",key = "'dic:queryDicTypeList_'+#form.groupKey")
    @Override
    public List<DicType> queryDicTypeList(QueryDicTypeListForm form) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("groupKey",form.getGroupKey());
        qw.orderByAsc("sortNum");
        List<DicType> list = dicTypeMapper.selectList(qw);
        return list;
    }

    @Override
    public boolean existByTypeKey(String typeKey,Long dicTypeId) {
        QueryWrapper qw = new QueryWrapper();
        qw.select("id");
        qw.eq("typeKey",typeKey);
        DicType dicType = dicTypeMapper.selectOne(qw);
        if(dicType==null){
            return false;
        }
        if(dicTypeId!=null && dicType.getId().equals(dicTypeId)){
            return false;
        }
        return true;
    }

    @Transactional
    @Override
    public void deleteByDicTypeId(Long dicTypeId) {
        DicType dicType = dicTypeMapper.selectById(dicTypeId);
        if(dicType!=null){
            QueryWrapper qw = new QueryWrapper();
            qw.eq("typeKey",dicType.getTypeKey());
            dicInfoMapper.delete(qw);
            dicTypeMapper.deleteById(dicTypeId);
            dicCacheService.dicTypeCacheUpdate(dicType.getGroupKey());
        }
    }

    @Transactional
    @Override
    public void updateByForm(UpdateDicTypeForm form) {
        DicType dicType = dicTypeMapper.selectById(form.getId());
        if(StrUtil.isNotEmpty(form.getTypeKey()) && !dicType.getTypeKey().equals(form.getTypeKey())){
            dicInfoMapper.updateTypeKey(form.getTypeKey(),dicType.getTypeKey());
        }
        JSONObject jsonObject = JSONUtil.parseObj(form);
        dicTypeMapper.updateDicType(jsonObject);
        dicCacheService.dicTypeCacheUpdate(dicType.getGroupKey());
    }

    @Override
    public String queryIdPath(Long parentId) {
        if(parentId==null || parentId == BaseConstant.LONG_0){
            return null;
        }
        DicType dicType = dicTypeMapper.selectById(parentId);
        if(dicType == null){
            return null;
        }
        List<String> idList = new ArrayList<>();
        idList.add(dicType.getId().toString());
        String idPath = this.queryIdPath(dicType.getParentId());
        if(StrUtil.isNotEmpty(idPath)){
            idList.add(idPath);
        }
        return CollUtil.join(idList,BaseConstant.SPITS_STR4);
    }
}
